﻿
var actionURL = "ashx/YBQHandler.ashx";
var uid;
var cid;
var dtOpstart;
var dtOpend;
var filterObj;
var ch = new Array("", "", "", "");
var XQHID = "";
var alevel
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return unescape(r[2]); return null; //返回参数值
}
var mylayout = {
    init: function (size) {
        $('#layout').width(size.width - 4).height(size.height - 4).layout();
        var center = $('#layout').layout('panel', 'center');
        center.panel({
            onResize: function (w, h) {
                $('#histList').datagrid('resize', { width: w, height: h });
            }
        });
    },
    resize: function (size) {
        mylayout.init(size);
        $('#layout').layout('resize');
    }
};

$(function () {
    Highcharts.setOptions({
        lang: {
            contextButtonTitle: "图表导出菜单",
            decimalPoint: ".",
            downloadJPEG: "下载JPEG图片",
            downloadPDF: "下载PDF文件",
            downloadPNG: "下载PNG文件",
            downloadSVG: "下载SVG文件",
            drillUpText: "返回 {series.name}",
            loading: "加载中",
            months: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
            noData: "没有数据",
            numericSymbols: ["千", "兆", "G", "T", "P", "E"],
            printChart: "打印图表",
            resetZoom: "恢复缩放",
            resetZoomTitle: "恢复图表",
            shortMonths: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
            thousandsSep: ",",
            weekdays: ["星期一", "星期二", "星期三", "星期三", "星期四", "星期五", "星期六", "星期天"]
        }
    });

    var size = { width: $(window).width(), height: $(window).height() };
    $('#dtOpstart,#dtOpend').datebox({ width: 100 });
    mylayout.init(size);
    uid = getUrlParam("uid");
    DicCategory.bindTree(size);
    autoResize({ dataGrid: '#histList', gridType: 'datagrid', callback: grid.bind, height: 0 });
    //ChartJS.init(ch[0], ch[1], ch[2], ch[3]);
    $('#a_search').click(search);
    $('#a_export').click(function () {
        Export('导出excel', $('#histList'));
    });
    $(window).resize(function () {
        size = { width: $(window).width(), height: $(window).height() };
        mylayout.resize(size);
    });
});
function ConvertJSONDateToJSDate(jsondate) {
    var date = new Date(parseInt(jsondate, 10));
    return date;
}
var chartInstance;
var ChartJS = {
    init: function (chs) {
        var s = "[";       
        $.each(chs, function () {
            s = s + "{name:'" + this + "',data:[]},";
        });
        s = s.substr(0, s.length - 1);
        s = s + "]";
        chartInstance = new Highcharts.Chart({
            chart: {
                renderTo: 'chartArea',
                type: 'line'

            },
            credits: {
                enabled: false
            },
            exporting: {
                enabled: true
            },
            title: {
                text: '靶标害虫区域曲线',
                x: -20 //center
            },
            xAxis: {
                categories: []
            },
            yAxis: {
                title: {
                    text: '数量 (只)'
                },
                plotLines: [{
                    value: 0,
                    width: 1,
                    color: '#808080'
                }]
            },
            plotOptions: {
            line: {
                dataLabels: {
                    enabled: true
                }
            }
        },
            legend: {
                layout: 'vertical',
                align: 'right',
                verticalAlign: 'middle',
                borderWidth: 0
            },
            series: eval(s)
        });
    }
};

function contains(string, substr, isIgnoreCase) {
    if (isIgnoreCase) {
        string = string.toLowerCase();
        substr = substr.toLowerCase();
    }
    var startChar = substr.substring(0, 1);
    var strLen = substr.length;
    for (var j = 0; j < string.length - strLen + 1; j++) {
        if (string.charAt(j) == startChar) //如果匹配起始字符,开始查找
        {
            if (string.substring(j, j + strLen) == substr) //如果从j开始的字符与str匹配，那ok
            {
                return true;
            }
        }
    }
    return false;
}

var DicCategory = {
    bindTree: function (winSize) {
        $('#myharm').combotree({
            url: actionURL + "?action=myharm",
            valueField: 'id',
            textField: 'text',
            panelHeight: 'auto',
            checkbox: true,
            onlyLeafCheck: true,
            lines: true,
            multiple: true
        });
        $('#ybqNum').tree({
            url: '/ashx/AreaHandler.ashx?isadd=1',
            onLoadSuccess: function (node, data) {
                if (data.length == 0) {
                    $('#noCategoryInfo').fadeIn();
                }
                $('body').data('categoryData', data);
            },
            onBeforeExpand: function (node) {
                $('#ybqNum').tree('options').url = "/ashx/AreaHandler.ashx?keyid=" + node.id;
            },
            onClick: function (node) {
                var id = node.id;
                cid = id;
                dtOpstart = $('#dtOpstart').datebox('getValue');
                dtOpend = $('#dtOpend').datebox('getValue');
                var mharms = $("#myharm").combotree("getText");
                if (mharms != "") {
                    var chs = mharms.split(',');
                    var s = "[[";
                    s = s + "{field:'area',title:'地区',width:160}";
                    $.each(chs, function () {
                        s = s + ",{field:'" + this + "',title:'" + this + "',width:160}";
                    });
                    s = s + "]]";
                    $('#histList').datagrid({
                        title: node.text + "数据统计",
                        pagination: true,
                        pageSize: 35,
                        url: '/status_ybq/ashx/YBQHandler.ashx?Action=count',
                        queryParams: { ulevel: node.id, harms: mharms, stime: dtOpstart, etime: dtOpend },
                        pageList: [10, 35, 50, 100],
                        columns: eval(s),
                        onLoadSuccess: function (data) {
                            ChartJS.init(chs);
                            var md;
                            var t1 = "";
                            $.each(data.rows, function (k, v) {
                                md = v["area"];
                                t1 += "'" + md + "',";

                            });
                            t1 = t1 = "[" + t1 + "]";
                            chartInstance.xAxis[0].setCategories(eval(t1));
                            for (var i = 0; i < chs.length; i++) {
                                t1 = "";
                            $.each(data.rows, function (k, v) {
                                md = v["area"];                                
                                t1 += "[" + v[chs[i]] + "],";                                

                            });
                             t1 = "[" + t1 + "]"; 
                             chartInstance.series[i].setData(eval(t1));
                            }
                        }                       
                    });
                }
                else {
                    msg.error('对不起，请选择要统计的虫子！');
                }
            }

        });
    },
    reload: function () {
        $('#ybqNum').tree('reload');
    },
    getSelected: function () {
        return $('#ybqNum').tree('getSelected');
    }
};

var grid = {
    bind: function (winSize) {
        $('#histList').datagrid({
            toolbar: '#toolbar',
            //url: '/status_ybq/ashx/YBQHandler.ashx?Action=detail',
            title: "靶标害虫设备历史数据列表",
            iconCls: 'icon icon-list',
            width: winSize.width - 200,
            height: winSize.height - 240,
            nowrap: true, //折行
            rownumbers: true, //行号
            striped: true, //隔行变色
            idField: 'keyid', //主键
            singleSelect: true, //单选
            fitColumns: true,
            columns: [[                
                     { title: '<b>区域</b>', field: 'devID', width: 100, align: 'center' }
            ]],
            pagination: true,
            pageSize: 10,
            pageList: [10, 20, 40, 50, 10000]
        });
    },
    getSelectedRow: function () {
        return $('#histList').datagrid('getSelected');
    },
    reload: function () {
        $('#histList').datagrid('clearSelections').datagrid('reload', { devID: cid });
    }
};
function createParam(action, keyid) {
    var o = {};
    var query = top.$('#uiform').serializeArray();
    query = convertArray(query);
    o.jsonEntity = JSON.stringify(query);
    o.action = action;
    o.keyid = keyid;
    return "json=" + JSON.stringify(o);
}


function search() {
    if (cid !=null) {
        dtOpstart = $('#dtOpstart').datebox('getValue');
        dtOpend = $('#dtOpend').datebox('getValue');
        var mharms = $("#myharm").combotree("getText");
        if (mharms != "") {
            var chs = mharms.split(',');
            var s = "[[";
            s = s + "{field:'area',title:'地区',width:160}";
            $.each(chs, function () {
                s = s + ",{field:'" + this + "',title:'" + this + "',width:160}";
            });
            s = s + "]]";

            $('#histList').datagrid({
                url: '/status_ybq/ashx/YBQHandler.ashx?Action=count',
                queryParams: { ulevel: cid, harms: mharms, stime: dtOpstart, etime: dtOpend },
                columns: eval(s),
                onLoadSuccess: function (data) {
                    ChartJS.init(chs);
                    var md;
                    var t1 = "";
                    $.each(data.rows, function (k, v) {
                        md = v["area"];
                        t1 += "'" + md + "',";

                    });
                    t1 = t1 = "[" + t1 + "]";
                    chartInstance.xAxis[0].setCategories(eval(t1));
                    for (var i = 0; i < chs.length; i++) {
                        t1 = "";
                        $.each(data.rows, function (k, v) {
                            md = v["area"];
                            t1 += "[" + v[chs[i]] + "],";

                        });
                        t1 = "[" + t1 + "]";
                        chartInstance.series[i].setData(eval(t1));
                    }
                }
            });
        }
        else {
            msg.error('对不起，请选择要统计的虫子！');

        }
    }
    else {
        msg.error('对不起，请选择要统计的区域！');
    }
}

